<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/26 0026
 * Time: 下午 21:00
 */

namespace Home\Controller;


use Org\Util\Cart;
use Think\Controller;

class ContentController extends BaseController
{
    public function index(){

        $gid=i('get.gid');
//        dd($gid);
        $Conf=[
            'title'=>'列表页',
            'js'=>['list'],
        ];

        $this->assign('Conf',$Conf);
//商品属性 ，类型属性
        $field=m('typeattribute')->alias('ta')
            ->join('__GOODS_ATTR__ ga on ta.taid=ga.taid')
            ->where("gid={$gid} and ta.ttype=2")->select();
        $data=[];
        foreach ($field as $k=>$v){
            $data[$v['tname']][] = $v;
        }
//       dd($field);
        $this->assign('data',$data);
        //查找大中小图
        $detailData=m('detail')->where("gid=$gid")->find();

        $big=explode('|',$detailData['big']);

        $detailData['big']=[];
        foreach ($big as $k=>$v){

            $detailData['big'][]=$v;
        }
//        dd($detailData);
        $this->assign('detailData',$detailData);
//查找商品表  价格 商品价格
       $goodsData=m('goods')->where("gid=$gid")->find();
//       dd($goodsData);
       $this->assign('goodsData',$goodsData);

        $this->display();

    }

    public function ajaxGetTotal(){
        if (IS_AJAX){
            $gid=i('post.gid');

            $combine=rtrim(I('post.combine'),',');
            $gaid=explode(',',$combine);
          $added='';
            foreach ($gaid as $v){
                $s[]=m('goods_attr')->where("gaid={$v}")->getField('added');
            }
            foreach ($s as $vv){
                $added+=$vv;
            }

//            dd($added);
                $marketprice=M('goods')->where("gid={$gid}")->getField('marketprice');
            $guiztotal=$added+$marketprice;



            //在货品列表中查找数据
            $data = m('goods_list')->where("gid={$gid} and combine='{$combine}'")->find();
            //如果￥data为空则 不能购买
            if (!$data){
                $this->ajaxReturn(['valid'=>0,'total'=>0]);exit;
            }
            if ($data['invengory']==0){
                //说明库存为0
                $this->ajaxReturn(['valid'=>0,'total'=>0]);exit;
            }
            $this->ajaxReturn(['valid'=>1,'total'=>$data['invengory'],     'glid'=>$data['glid'],'number'=>$data['number'],'guiztotal'=>$guiztotal]);exit;
        }
    }

    public function ajaxAddCart(){
        if (IS_AJAX){
        $data['gid']=$_POST['gid'];
        $count=$_POST['count'];
        $data['num']=$_POST['num'];
        $data['options']=$_POST['options'];
        $data['prive']=$_POST['prive'];
//        dd($data['options']);die;
        $field=m('goods')->where("gid={$data['gid']}")->field('gname,pic,shopprice')->find();
//        dd($filed);
            $data = [
                'id'      => $data['gid'] , // 商品 ID
                'name'    => $field['gname'] ,// 商品名称
                'num'     => $data['num'] , // 商品数量
                'price'   => $data['prive'] , // 商品价格
                'glid'    => $_POST['glid'],
                'number'  => $_POST['number'],
                'options' => [
                    'options'=> $data['options'],
                    'img'=>$field['pic'],
                    'count'=>$count,
                ]// 其他参数如价格、颜色、可以为数组或字符串

            ];
            (new Cart())->add($data);

            echo 1;die;
        }
    }

}